package HibernateOrederManagement.app;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import HibernateOrderManagerment.bean.GoodsStatus;

public class GoodsStatusDao {
private Session session;
	
	public GoodsStatusDao(Session session){
		this.session=session;
	}
	
	public List<GoodsStatus> list(){
		Query<GoodsStatus> query=session.createQuery("from GoodsStatus",GoodsStatus.class);
		
		return query.getResultList();
	}
	
	public GoodsStatus getGoodsStatusById(long id){
		return session.get(GoodsStatus.class, id);
	}
	
	public GoodsStatus getByCode(String code){
		Query<GoodsStatus> query =session.createQuery("from GoodsStatus where code=:code",GoodsStatus.class);
		query.setParameter("code", code);
		return query.getSingleResult();
	}
	public GoodsStatus getByName(String name){
		Query<GoodsStatus> query =session.createQuery("from GoodsStatus where name=:name",GoodsStatus.class);
		query.setParameter("name", name);
		return query.getSingleResult();
	}
	public GoodsStatus listByDescription(String description){
		Query<GoodsStatus> query =session.createQuery("from GoodsStatus where description=:description",GoodsStatus.class);
		query.setParameter("description", description);
		return query.getSingleResult();
	}
	public void insert(GoodsStatus goodsStatus) {
		Transaction tx = session.beginTransaction();
		try {

			session.save(goodsStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(GoodsStatus goodsStatus){
		Transaction tx = session.beginTransaction();
		try {

			session.update(goodsStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}
	public void delete(GoodsStatus goodsStatus){
		Transaction tx = session.beginTransaction();
		try {
			
			session.delete(goodsStatus);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		
	}

}
